package com.teach;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class Test6 {

	public static void main(String[] args) {
		File f1 = new File("d:/upload/out2.txt");
		
		
		try(
			OutputStream out = new FileOutputStream(f1);
			BufferedOutputStream bout = new BufferedOutputStream(out);
				){
			byte[] bs = {65,66,67,68};
//			out.write(bs);//直接写入目标
			bout.write(bs);//先缓存起来
//			bout.close();//关闭的时候一定会将缓存中的数据写入目标
			bout.flush();//立即将缓存中的数据写入目标，清空缓存
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		File f2 = new File("d:/upload/in.txt");
		try (
			InputStream in = new FileInputStream(f2);
			BufferedInputStream bin = new BufferedInputStream(in);
				)
		{
			System.out.println(in.markSupported());//false 是否支持标记
			System.out.println(bin.markSupported());//true 
			
			System.out.println(bin.read());
			System.out.println(bin.read());
			bin.mark(1024);//做标记，并指定回退的最大长度，不能超出缓存大小
			System.out.println("bin.mark(1024);");
			System.out.println(bin.read());
			System.out.println(bin.read());
			System.out.println(bin.read());
			System.out.println(bin.read());
			System.out.println(bin.read());
			bin.reset();//回到标记位置
			System.out.println("bin.reset();");
			System.out.println(bin.read());
			System.out.println(bin.read());
			System.out.println(bin.read());
			System.out.println(bin.read());
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		byte[] bbs = null;
		
		try(
			InputStream in = new FileInputStream(f2);
			ByteArrayOutputStream baout = new ByteArrayOutputStream(1024);
			
				) {
			byte[] bs = new byte[1024];
			int len = in.read(bs);
			baout.write(bs, 0, len);
			bbs = baout.toByteArray();
			ByteArrayInputStream bain = new  ByteArrayInputStream(bbs);
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		
		
	}
}
